import { createRouter, createWebHistory } from 'vue-router'
import { ElMessage } from 'element-plus'
import MainLayout from '../layout/MainLayout.vue'
import AdminLayout from '../layout/AdminLayout.vue'
import Home from '../views/home/Home.vue'
import Login from '../views/account/Login.vue'
import Register from '../views/account/Register.vue'
import ClubList from '../views/club/ClubList.vue'
import ActivityList from '../views/activity/ActivityList.vue'
import NewsList from '../views/news/NewsList.vue'
import CommentList from '../views/comment/CommentList.vue' // 添加导入
import AdminComments from '../views/admin/AdminComments.vue'
import AdminUploads from '../views/admin/AdminUploads.vue'

const routes = [
  {
    path: '/',
    component: MainLayout,
    children: [
      {
        path: '',
        redirect: '/home'
      },
      {
        path: 'home',
        name: 'Home',
        component: Home
      },
      {
        path: 'clubs',
        name: 'ClubList',
        component: ClubList
      },
      {
        path: 'activities',
        name: 'ActivityList',
        component: ActivityList
      },
      {
        path: 'news',
        name: 'NewsList',
        component: NewsList
      },
      {
        path: 'comments',
        name: 'CommentList',
        component: CommentList // 使用正确的组件
      }
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/register',
    name: 'Register',
    component: Register
  },
  {
    path: '/admin',
    component: AdminLayout,
    meta: { requiresAdmin: true },
    children: [
      {
        path: '',
        redirect: '/admin/dashboard'
      },
      {
        path: 'dashboard',
        name: 'AdminDashboard',
        component: () => import('../views/admin/AdminDashboard.vue')
      },
      {
        path: 'clubs',
        name: 'AdminClubs',
        component: () => import('../views/admin/AdminClubs.vue')
      },
      {
        path: 'activities',
        name: 'AdminActivities',
        component: () => import('../views/admin/AdminActivities.vue')
      },
      {
        path: 'news',
        name: 'AdminNews',
        component: () => import('../views/admin/AdminNews.vue')
      },
      {
        path: 'users',
        name: 'AdminUsers',
        component: () => import('../views/admin/AdminUsers.vue')
      },
      {
        path: '/admin/comments',
        name: 'AdminComments',
        component: AdminComments
      },
      {
        path: 'uploads',
        name: 'AdminUploads',
        component: AdminUploads,
        meta: { 
          requiresAuth: true,
          requiresAdmin: true,
          title: '文件管理'
        }
      },
      {
        path: '/admin/approvals',
        name: 'AdminApprovals',
        component: () => import('../views/admin/AdminApprovals.vue'),
        meta: {
          requiresAuth: true,
          requiresAdmin: true,
          title: '审批管理'
        }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  // 改回使用 localStorage 获取登录信息
  const token = localStorage.getItem('token')
  const userInfo = JSON.parse(localStorage.getItem('userInfo') || '{}')
  
  if (to.path === '/login' || to.path === '/register') {
    next()
    return
  }
  
  if (!token) {
    next('/login')
    return
  }

  if (to.matched.some(record => record.meta.requiresAdmin)) {
    if (!userInfo.admin) {
      ElMessage.error('需要管理员权限')
      next('/home')
      return
    }
  }
  
  next()
})

export default router